package apps.reading.ws.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import apps.reading.db.FavoriteBean;
import apps.reading.db.WebSourceBean;
import apps.reading.db.WebSourceService;
import core.tool.common.StringHelper;
import core.tool.log.log4j.EasyLogger;
import core.web.spring.SpringMVCUtils;

@Controller
@RequestMapping("/wread")
public class WebReadingController {
	
    @Autowired
	private WebSourceService webSourceService;
	
	@RequestMapping("/read")
	public ModelAndView read() {
		ModelAndView mav = new ModelAndView();
		PageRequest pageReqeust=new PageRequest(1,7);
		Page<WebSourceBean> pageResult=webSourceService.findBySourceTypePager(WebSourceBean.SOURCE_TYPE_CNBLOG, pageReqeust);	
		mav.addObject("cnblogList", pageResult.getContent());
		mav.setViewName("reading_client/web/index");
		return mav;	
	}
	
	
	
	@RequestMapping("/getPagerList")
	public @ResponseBody Object getPagerList(@RequestParam("sourceType")String sourceType,@RequestParam("type")String type,@RequestParam("page")int page,@RequestParam("size")int size) {
		EasyLogger.debug("sourceType："+sourceType+"type："+type);
		EasyLogger.debug("页码："+page+"记录条数："+size);
		Sort sort=new Sort(Direction.DESC,"publicDate");
		PageRequest pageReqeust=new PageRequest(page,size,sort);
		ModelAndView mav = new ModelAndView();
		Page<WebSourceBean> pageResult=webSourceService.findBySourceTypePager(sourceType, type,pageReqeust);
        Map attributes = new HashMap();
        attributes.put("total", pageResult.getTotalElements());
        List<WebSourceBean> list=pageResult.getContent();
        Map<String, String> mapper=new HashMap<String, String>();
        mapper.put("id", "id");
        mapper.put("title", "title");
		List<Map<String, Object>> returnList=SpringMVCUtils.getMapperJSON(list, mapper);
        attributes.put("rows", returnList);        
		return attributes;
	}		
	
	@RequestMapping("/readItem")
	public ModelAndView read(@RequestParam("id")String id,@RequestParam("userId")String userId) {
		ModelAndView mav = new ModelAndView();
		WebSourceBean webSourceBean=webSourceService.getById(id);
		boolean favoriteFlag=webSourceService.existsFavorite(id, userId);
		mav.addObject("item", webSourceBean);
		mav.addObject("favoriteFlag",favoriteFlag);
		mav.setViewName("reading_client/web/readitem");
		return mav;	
	}
	
	@RequestMapping("/addItem")
	public ModelAndView addItem(@RequestParam("id")String id,@RequestParam("content")String content) {
		ModelAndView mav = new ModelAndView();
		WebSourceBean webSourceBean=new WebSourceBean();
		webSourceBean.setId(StringHelper.getUUID());
		if(!StringHelper.isEmpty(id)) {
			webSourceBean=webSourceService.getById(id);
		}
		webSourceBean.setTitle(StringHelper.getUUID());
		webSourceBean.setContent(content);
		webSourceBean.setSourceType(WebSourceBean.SOURCE_TYPE_SELF);
		webSourceBean.setType(WebSourceBean.TYPE_NEWS1);
		webSourceService.save(webSourceBean);
		mav.setViewName("reading_client/web/type_index");
		return mav;	
	}	
	
	@RequestMapping("/addFavorite")
	public @ResponseBody Object addFavorite(@ModelAttribute FavoriteBean favoriteBean) {
		webSourceService.addFavorite(favoriteBean);
		return 1;
	}	
	
	
	@RequestMapping("/cancelFavorite")
	public @ResponseBody Object cancelFavorite(@ModelAttribute FavoriteBean favoriteBean) {
		webSourceService.cancelFavorite(favoriteBean);
		return 1;
	}	
	
	@RequestMapping("/getFPagerList")
	public @ResponseBody Object getFavoritePagerList(@RequestParam("userId")String userId,@RequestParam("page")int page,@RequestParam("size")int size) {
		EasyLogger.debug("页码："+page+"记录条数："+size);
		Page<FavoriteBean> pageResult=webSourceService.findByUserIdPager(userId, page, size);
        Map attributes = new HashMap();
        attributes.put("total", pageResult.getTotalElements());
        List<FavoriteBean> list=pageResult.getContent();
        Map<String, String> mapper=new HashMap<String, String>();
        mapper.put("sourceid", "id");
        mapper.put("userid", "userid");
        mapper.put("sourcetitle", "title");
		List<Map<String, Object>> returnList=SpringMVCUtils.getMapperJSON(list, mapper);
        attributes.put("rows", returnList);
		return attributes;
	}	
	
	
}
